﻿using System;
using System.Collections.Generic;
using System.Web;

namespace DBUtility
{
    /// <summary>
    /// 生成 json 字符串
    /// 数据格式：{"status":0,"msg":"SiteID錯誤","data":[{},{}] }
    /// </summary>
    public class JsonHelper
    {
        /// <summary>
        /// 状态 1=成功 0=失敗
        /// </summary>
        private int status;

        /// <summary>
        /// 消息(錯誤原因，失敗原因)
        /// </summary>
        private string msg;

        /// <summary>
        /// 数据(數組)
        /// </summary>
        private string data;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="msg">消息</param>
        /// <param name="data">数据</param>
        public JsonHelper(int status, string msg, string data)
        {
            this.status = status;
            this.msg = msg;
            this.data = data;
        }

        #region 1.0 生成 Json消息 字符串 +static string AjaxMsg
        /// <summary>
        /// 输出 狀態為成功的 Json 字符串
        /// </summary>
        /// <returns></returns>
        public static void Success()
        {
            Response(1, string.Empty, string.Empty);
        }

        /// <summary>
        /// 输出 狀態為成功的 Json 字符串
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static void Success(string data)
        {
            Response(1, string.Empty, data);
        }

        /// <summary>
        /// 输出 狀態為成功的 Json 字符串
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static void Success(string msg, string data)
        {
            Response(1, msg, data);
        }

        /// <summary>
        /// 输出 狀態為失敗的 Json 字符串
        /// </summary>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        public static void Fail(string msg)
        {
            Response(0, msg, string.Empty);
        }

        /// <summary>
        /// 输出 狀態為失敗的 Json 字符串
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static void Fail(string msg, string data)
        {
            Response(0, msg, data);
        }

        /// <summary>
        /// 输出 Json 字符串
        /// </summary>
        /// <param name="strJson">json字符串</param>
        /// <returns></returns>
        public static void Response(string strJson)
        {
            HttpContext.Current.Response.ContentType = "application/json";
            HttpContext.Current.Response.Write(strJson);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 输出 Json 字符串
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        public static void Response(int status, string msg)
        {
            Response(status, msg, string.Empty);
        }

        /// <summary>
        /// 输出 Json 字符串
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="msg">消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static void Response(int status, string msg, string data)
        {
            msg = msg ?? string.Empty;//如果msg为null则赋值为空字符串

            if (string.IsNullOrEmpty(data))
            {
                data = "[]";
            }

            string strJson = null;
            if (data.StartsWith("[") && data.EndsWith("]"))//头尾都包含中括号
            {
                strJson = "{\"Status\":" + status + ",\"Msg\":\"" + msg + "\",\"Data\":" + data + "}";
            }
            else//头尾都不包含中括号
            {
                strJson = "{\"Status\":" + status + ",\"Msg\":\"" + msg + "\",\"Data\":[" + data + "]}";
            }

            HttpContext.Current.Response.ContentType = "application/json";
            HttpContext.Current.Response.Write(strJson);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 输出 Json 字符串
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="msg">消息</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static void Response(int status, string msg, int pageIndex, int pageSize, long pageCount, string data)
        {
            msg = msg ?? string.Empty;//如果msg为null则赋值为空字符串

            if (string.IsNullOrEmpty(data))
            {
                data = "[]";
            }

            string strJson = null;
            if (data.StartsWith("[") && data.EndsWith("]"))//头尾都包含中括号
            {
                strJson = "{\"Status\":" + status + ",\"Msg\":\"" + msg + "\",\"Page\":" + pageIndex + ",\"PageSize\":" + pageSize + ",\"PageCount\":" + pageCount + ",\"Data\":" + data + "}";
            }
            else//头尾都不包含中括号
            {
                strJson = "{\"Status\":" + status + ",\"Msg\":\"" + msg + "\",\"Page\":" + pageIndex + ",\"PageSize\":" + pageSize + ",\"PageCount\":" + pageCount + ",\"Data\":[" + data + "]}";
            }

            HttpContext.Current.Response.ContentType = "application/json";
            HttpContext.Current.Response.Write(strJson);
            HttpContext.Current.Response.End();
        }
        #endregion
    }
}
